package com.amtf.datafile2.core.txt;

import java.io.File;
import java.util.List;

import com.amtf.datafile2.DataAccessor;
import com.amtf.datafile2.DataException;
import com.amtf.datafile2.cfg.txt.TxtDataFieldDefinition;
import com.amtf.datafile2.cfg.txt.TxtDataTableDefinition;

public class TxtDataAccessor implements DataAccessor<TxtDataCatalog, TxtDataTable, TxtDataTableDefinition, TxtDataFieldDefinition> {
	// public static enum ACCESS_TYPE {
	// READ, WRITE, APPEND_WRITE,
	// // READ_AND_WRITE,
	// READ_AND_APPEND_WRITE;
	// }

	private TxtDataCatalog workDataCatalog = null;

	private File dirOrFile;
	// private ACCESS_TYPE accessType = ACCESS_TYPE.READ_AND_APPEND_WRITE;
	private String charsetName = "utf-8";

	public TxtDataAccessor(File dirOrFile, String charsetName) {
		this.dirOrFile = dirOrFile;
		this.charsetName = charsetName;
	}

	// public TxtDataAccessor(File file, ACCESS_TYPE accessType, String charsetName) throws DataException {
	// super(file, accessType, charsetName);
	// workDataCatalog = new TxtDataCatalog(file.getName(), file, accessType, charsetName);
	// }

	// public TxtDataCatalog openDataCatalog(ACCESS_TYPE accessType, String charsetName) throws DataException {
	// this.accessType = accessType;
	// this.charsetName = charsetName;
	//
	// String name = dirOrFile.isDirectory() ? dirOrFile.getName() : dirOrFile.getParentFile().getName();
	// workDataCatalog = new TxtDataCatalog(name, dirOrFile, accessType, charsetName);
	// return workDataCatalog;
	// }

	public TxtDataCatalog open() throws DataException {
		String name = dirOrFile.isDirectory() ? dirOrFile.getName() : dirOrFile.getParentFile().getName();
		workDataCatalog = new TxtDataCatalog(name, dirOrFile, charsetName);
		return workDataCatalog;
	}

	public void commit() throws DataException {
		List<TxtDataTable> all = workDataCatalog.getDataTables();
		for (TxtDataTable txtDataTable : all) {
			txtDataTable.close();
		}
	}

	public void close() throws DataException {
		workDataCatalog.close();
	}

	//
	// public TxtDataAccessor(File file, ACCESS_TYPE accessType) throws IOException {
	// super(file, accessType);
	// }
	//
	// public TxtDataAccessor(InputStream inStream, OutputStream outStream, String charsetName) throws IOException {
	// super(inStream, outStream, charsetName);
	// }
	//
	// public TxtDataAccessor(InputStream inStream, OutputStream outStream) throws IOException {
	// super(inStream, outStream);
	// }
	//
	// public TxtDataAccessor(InputStream inStream) throws IOException {
	// super(inStream);
	// }
	//
	// public TxtDataAccessor(OutputStream outStream) throws IOException {
	// super(outStream);
	// }
	//
	// public TxtDataAccessor(File file, TxtDataRecordDefinition definition) throws IOException {
	// this(file, ACCESS_TYPE.READ_AND_WRITE);
	// }

}
